namespace LocalBoard.Web.UserControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Security.Principal;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Web.Security;

	/// <summary>
	///		Summary description for login.
	/// </summary>
	public partial class login : System.Web.UI.UserControl
	{
		private string _mode = "Normal";
		public string Mode
		{
			get {return _mode;}
			set {_mode = value;}
		}

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(_mode.ToUpper() == "SMALL")
				ShowSmall();
			else
				ShowNormal();

			errorTable.Visible = false;
			if(Request.IsAuthenticated)
			{
				loginBox.Visible = false;
			}
			else
			{
				loginBox.Visible = true;
			}
		}

		private void ShowNormal()
		{
			userIdLabel.Visible = true;
			userIdLabelSmall.Visible = false;
			passwordLabel.Visible = true;
			passwordLabelSmall.Visible = false;
			rememberIdLabel.Visible = true;
			rememberIdLabelSmall.Visible = false;
		}

		private void ShowSmall()
		{
			userIdLabel.Visible = false;
			userIdLabelSmall.Visible = true;
			passwordLabel.Visible = false;
			passwordLabelSmall.Visible = true;
			rememberIdLabel.Visible = false;
			rememberIdLabelSmall.Visible = true;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion

		protected void loginButton_Click(object sender, System.EventArgs e)
		{
			if(Security.AuthenticateUser(userId.Text, password.Text))
			{
				// Use security system to set the UserID within a client-side Cookie
				FormsAuthentication.SetAuthCookie(userId.Text, rememberUserId.Checked);
				string redirectUrl = FormsAuthentication.GetRedirectUrl(userId.Text, rememberUserId.Checked);
				if(Request.Path.EndsWith("login.aspx"))
					Response.Redirect(Request.ApplicationPath + "/home.aspx");
				else if(redirectUrl.EndsWith("default.aspx"))
					Response.Redirect(Request.RawUrl);
			}
			else
				errorTable.Visible = true;
		}
	}
}
